package com.guinea.core.tag;

import java.io.IOException;

import javax.servlet.jsp.JspException;

import modules.system.entity.Attach;

import org.apache.struts.taglib.TagUtils;

import com.guinea.core.SpringContextHolder;
import com.guinea.core.utils.ReflectionUtils;
import com.guinea.core.utils.StringUtils;

import ex.system.service.AttachServiceEx;

public class AttachTag extends CommonTag {

	private static final long serialVersionUID = -774840953985543612L;

	public int doEndTag() throws JspException {
		String attachAttr = "";
		Long attachId = (Long) TagUtils.getInstance().lookup(pageContext, name,
				property, null);
		AttachServiceEx attachServiceEx = (AttachServiceEx) SpringContextHolder
				.getBean("attachServiceEx");
		if (attachId != null) {
			Attach attach = attachServiceEx.getAttach(attachId);
			if (attach != null) {
				if(StringUtils.isEmpty(attr)){
					attachAttr = attach.getWebPath();
				}else{
					Object x = ReflectionUtils.getFieldValue(attach, attr);
					if (x != null)
						attachAttr = x.toString();	
				}
			}
		}
		try {
			pageContext.getOut().print(attachAttr);
		} catch (IOException e) {
			e.printStackTrace();
		}

		return super.doEndTag();
	}

}
